package priv.dajie.tzzq.verifyRec;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.ByteArrayBody;
import org.apache.http.message.BasicNameValuePair;
import priv.dajie.tzzq.BaseStatic;
import priv.dajie.tzzq.model.RecResponse;
import priv.dajie.tzzq.common.Tools;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class VerifyRec extends BaseStatic {
    private static String pdID;
    private static String pdKey;
    private static String appID;
    private static String appKey;

    static {
        Properties properties = new Properties();
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(System.getProperty("user.dir")+ "/config/fateadm.properties");
            properties.load(inputStream);
            pdID = properties.getProperty("pdID");
            pdKey = properties.getProperty("pdKey");
            appID = properties.getProperty("appID");
            appKey = properties.getProperty("appKey");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(inputStream!=null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    public static RecResponse capreg(byte[] buffer, String type){ // 待测试 2019.5.11
        HttpPost httpPost = new HttpPost("http://pred.fateadm.com/api/capreg");
        String timestamp= ""+System.currentTimeMillis()/1000;
        MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create().setMode(HttpMultipartMode.RFC6532);
        entityBuilder.addTextBody("user_id", pdID);
        entityBuilder.addTextBody("timestamp", timestamp);
        entityBuilder.addTextBody("sign", getSign(timestamp));
        entityBuilder.addTextBody("predict_type", type);
        entityBuilder.addTextBody("up_type", "mt");
        entityBuilder.addBinaryBody("img_data", buffer);
        entityBuilder.addPart("img_data",new ByteArrayBody(buffer, "data.jpg"));
        HttpEntity httpEntity = entityBuilder.build();
        httpPost.setEntity(httpEntity);
        String jsonStr = open(httpPost);
        JSONObject json = JSON.parseObject(jsonStr);
        String error = "";
        String code = "";
        String requestID = json.getString("RequestId");
        if(json.getInteger("RetCode")!=0)
            error = json.getString("ErrMsg");
        String res = json.getString("RspData");
        Pattern pattern = Pattern.compile(": \\\"(.*)\\\"}");
        Matcher matcher = pattern.matcher(res);

        if (matcher.find())
            code =  matcher.group(1);
        return new RecResponse(code, requestID, error);
    }

    public static RecResponse capreg(byte[] buffer){
        return capreg(buffer, "20500");
    }

    public static boolean capjust(RecResponse recResponse){ // 订单退款
        String timestamp = ""+System.currentTimeMillis()/1000;
        HttpPost httpPost = new HttpPost();
        List<NameValuePair> data = new ArrayList<>();
        data.add(new BasicNameValuePair("user_id", pdID));
        data.add(new BasicNameValuePair("timestamp", timestamp));
        data.add(new BasicNameValuePair("sign", getSign(timestamp)));
        data.add(new BasicNameValuePair("request_id",   recResponse.getRequestID()));
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(data));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String jsonStr = open(httpPost);
        JSONObject json = JSON.parseObject(jsonStr);
        return json.getInteger("RetCode") == 0; // 判断返回是否为0
    }

    private static String getSign(String timestamp){
        return Tools.md5(pdID+timestamp+Tools.md5(timestamp+pdKey));
    }

    private static String getASign(String timestamp){
        return Tools.md5(appID+timestamp+Tools.md5(timestamp+appKey));
    }

}
